package com.pharmacy.LogManage.service;

import com.pharmacy.LogManage.dao.OperationLogMapper;
import com.pharmacy.LogManage.entity.OperationLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class OperationLogServiceImpl implements OperationLogService {

    @Autowired
    private OperationLogMapper operationLogMapper;

    @Override
    public List<OperationLog> getAllLogs() {
        return operationLogMapper.findAllLogs();
    }

    @Override
    public List<OperationLog> getLogsByDateRange(Date startDate, Date endDate) {
        return operationLogMapper.findLogsByDateRange(startDate, endDate);
    }

    @Override
    public List<OperationLog> getLogsByUserId(Integer userId) {
        return operationLogMapper.findLogsByUserId(userId);
    }

    @Override
    public List<OperationLog> getLogsByOperationType(String operationType) {
        return operationLogMapper.findLogsByOperationType(operationType);
    }

    @Override
    @Transactional
    public boolean addLog(OperationLog operationLog) {
        return operationLogMapper.insertLog(operationLog) > 0;
    }

    @Override
    @Transactional
    public void logOperation(Integer userId, String operationType, String targetType,
                             Integer targetId, String description) {
        OperationLog log = new OperationLog(userId, operationType, targetType, targetId, description);
        operationLogMapper.insertLog(log);
    }







    @Override
    @Transactional
    public boolean deleteLog(Integer id) {
        int result = operationLogMapper.deleteLogById(id);
        return result > 0;
    }

    @Override
    @Transactional
    public void clearLogs() {
        operationLogMapper.deleteAllLogs();
    }

    @Override
    public List<OperationLog> getLogsWithConditions(String operationType, String targetType,
                                                    Integer userId, Date startDate, Date endDate) {
        return operationLogMapper.findLogsWithConditions(operationType, targetType, userId, startDate, endDate);
    }

}